package com.panopset.gui;

import static com.panopset.Util.getExtension;
import static com.panopset.UtilIO.loadLinesFromResource;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.io.StringWriter;
import java.util.Collections;
import java.util.Map;
import java.util.TreeMap;
import java.util.Vector;
import java.util.Map.Entry;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import com.panopset.centralen.resource.Filter;
import com.panopset.gui.container.Dropper;
import com.panopset.swing.FontManager;

/**
 * Given a resource folder, allow the user to select any of the resources to
 * populate a text field, using a drop down.
 *
 * TODO: Give the user the option of saving the resource folder to their system
 * for editing.
 *
 * @author Karl Dinwiddie
 *
 */
public class ResourceTextArea {


    final Map<String, StringContainer> mp;

    public Map<String, StringContainer> getMap() {
        return mp;
    }
    /**
     * Test ResourceTextArea.
     *
     * @param s
     *            Not used.
     */
    public static void main(final String... s) {
        JFrame f = new JFrame();

        f.setLayout(new GridLayout(1, 1));
        f.add(
                new ResourceTextArea("title", "tip",
                        "/com/panopset/flywheel/gui").getPanel());
        f.setVisible(true);
    }

    /**
     * Filters.
     */
    private final Vector<Filter> filters = new Vector<Filter>();

    /**
     * @param f
     *            String filter.
     */
    public final void addFilter(final Filter f) {
        filters.add(f);
    }

    /**
     * Do any filtering of a loaded line.
     *
     * @param s
     *            String to filter.
     * @return Filtered result.
     */
    private String filter(final String s) {
        String rtn = s;
        for (Filter f : filters) {
            rtn = f.filter(rtn);
        }
        return rtn;
    }

    /**
     * @param title
     *            Title.
     * @param toolTipText
     *            Tool tip text.
     * @param resourceDir
     *            Resource directory.
     */
    public ResourceTextArea(final String title, final String toolTipText,
            final String resourceDir) {
        mp = loadTextResources(resourceDir);
        String defaultV = "";
        if (!mp.isEmpty()) {
            for (Entry<String, StringContainer> entry : mp.entrySet()) {
                defaultV = entry.getKey();
                continue;
            }
        }
        Dropper<String> dropper = new Dropper<String>(title, defaultV, "") {


            @Override
            protected void alertNewSelection(final String o) {
                ResourceTextArea.this.getInnerComponent().setText(
                        mp.get(o).value);
            }
            @Override
            public String getTopMenuTipText(final String sample) {
                return sample;
            }

            @Override
            public String getTopMenuName() {
                return "Select";
            }

            @Override
            public String getSelectionTipText(final String selection) {
                return selection;
            }

            @Override
            public String getMenuName(final String val, final int count) {
                return "Select";
            }

            @Override
            public Map<String, String> createSelectionList() {
                Map<String, String> map
                = Collections.synchronizedMap(new TreeMap<String, String>());
                for (String s : mp.keySet()) {
                    map.put(s, s);
                }
                return map;
            }
        };
        StringContainer dsc = mp.get(defaultV);
        if (dsc != null) {
            getInnerComponent().setText(dsc.value);
        }
        getPanel().add(BorderLayout.NORTH, dropper.getPanel());
        getPanel().add(BorderLayout.CENTER,
                new JScrollPane(getInnerComponent()));
    }

    public Map<String, StringContainer> loadTextResources(final String resourceDir) {
        Map<String, StringContainer> rtn = Collections
                .synchronizedMap(new TreeMap<String, StringContainer>());
        for (String r : loadLinesFromResource(resourceDir + "/index.lst")) {
            if (".txt".equalsIgnoreCase(getExtension(r.toString()))) {
                StringWriter sw = new StringWriter();
                for (String s : loadLinesFromResource(resourceDir + "/" + r)) {
                    sw.append(filter(s));
                    sw.append("\n");
                }
                rtn.put(r, new StringContainer(r, sw.toString()));
            }
        }
        return rtn;
    }

    private TTextArea ic;

    public TTextArea getInnerComponent() {
        if (ic == null) {
            ic = new TTextArea();
            FontManager.register(ic, FontManager.FontType.monospace);
        }
        return ic;
    }

    private TPanel p;

    public TPanel getPanel() {
        if (p == null) {
            p = new TPanel(new BorderLayout());
        }
        return p;
    }

    public String getValue() {
        return getInnerComponent().getText();
    }

    public static class StringContainer {
        public StringContainer(String r, String string) {
            name = r;
            value = string;
        }
        public String name;
        public String value;
    }
}
